//
//  WHTabBar.swift
//  WHWeiBo
//
//  Created by wangchen on 17/3/28.
//  Copyright © 2017年 王晨. All rights reserved.
//

import UIKit

class WHTabBar: UITabBar {
    
    lazy var composeButton: UIButton = {
        let button = UIButton()
        //设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: .normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: .highlighted)
        //设置不同状态的图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: .normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: .highlighted)
        
        //根据内容显示大小
        button.sizeToFit()
        
        return button
    }()
    
    //添加子视图空间
    func setupUi() {
        //添加撰写按钮
        addSubview(composeButton)
    }
    
    //调整子空间布局
    override func layoutSubviews() {
        super.layoutSubviews()
        composeButton.center.x = self.bounds.size.width * 0.5
        composeButton.center.y = self.bounds.size.height * 0.5
        let subViewWidth = self.bounds.size.width * 0.2
        var index = 0
        
        for subView in subviews {
            print(subView)
            if subView.isKind(of: NSClassFromString("UITabBarButton")!) {
                
                subView.bounds.size.width = subViewWidth
                index += 1
                if index == 3 {
                    index += 1
                }
                subView.center.x = subViewWidth * CGFloat(index) - subViewWidth * 0.5
                
                
                
                
            }
            
            
            
        }
        
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUi()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
